﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace FIM
{
    public class CManager : GameComponent
    {
        public GamePadState[] CurrentGamepad { get; private set; }
        public GamePadState[] PreviousGamepad { get; private set; }
        public KeyboardState CurrentKeyboard { get; private set; }
        public KeyboardState PreviousKeyboard { get; private set; }
        public MouseState CurrentMouse { get; private set; }
        public MouseState PreviousMouse { get; private set; }
        public int MouseOriginX { get; private set; }
        public int MouseOriginY { get; private set; }

        public CManager(Game game)
            : base(game)
        {
            CurrentGamepad = new GamePadState[4];
            PreviousGamepad = new GamePadState[4];
            CurrentMouse = PreviousMouse = Mouse.GetState();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < 4; i++)
            {
                PreviousGamepad[i] = CurrentGamepad[i];
                CurrentGamepad[i] = GamePad.GetState((PlayerIndex)i);
            }

            PreviousKeyboard = CurrentKeyboard;
            CurrentKeyboard = Keyboard.GetState();

            PreviousMouse = CurrentMouse;
            CurrentMouse = Mouse.GetState();
            if (Game.IsActive)
                Mouse.SetPosition(MouseOriginX, MouseOriginY);
            else
            {
                MouseOriginX = CurrentMouse.X;
                MouseOriginY = CurrentMouse.Y;
            }
        }
    }
}
